<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Admin;
use think\Request;
use app\util\File;
use think\Db;
use app\admin\model\ProvCityArea;
use app\admin\model\TradingCity;
use app\admin\model\TradingLabel;
use app\admin\model\Staff;
use app\util\Utils;
use think\Validate;
use app\admin\model\StaffBindStore;
use app\admin\model\StaffCtm;
use app\admin\model\Order;
use app\admin\model\GoodsOtherCategory;
use app\admin\model\ItemToGoods;
use app\admin\model\CtmCollect;
use app\admin\model\StoreCtm;
use app\admin\model\Custom;
use app\admin\model\Yuyue;
use app\admin\model\Store;
use app\admin\model\Item;

class PublicController extends CommonController {
    /*
     * 上传图片
     */

    public function uploadImg() {
        $modename = input('param.modeName');
        //上传头像
        $uploadFile = request()->file('file');
        if ($uploadFile) {
            //验证图片
            $file = new File();
            if (!$file->checkImg($uploadFile)) {
                $error = $file->getError();
                return $this->ajaxReturn(1, $error, null);
            }
        } else {
            return $this->ajaxReturn(3, '图片必须', null);
        }
        $photo = $file->uploadImage($uploadFile, $modename . '/');
        $mode = config('appconfig.app_production');
        if ($mode) {
            $publicFolder = 'kkmimg';
        } else {
            $publicFolder = 'testkkmimg';
        }
        $path = ROOT_PATH . '../' . $publicFolder;
        $dirPath = $path . '/kkmai/' . $modename . '/' . date('Ymd') . '/';
        if (!is_dir($dirPath)) {
            $flag = mkdir($dirPath, 0777, true);
        }
        $realPhoto = $file->getImageRealPath($photo);
        $curlPhoto = $file->curlUploadImage($publicFolder, pathinfo($photo, PATHINFO_DIRNAME), false, $realPhoto);
        if (!$curlPhoto) {
            return $this->ajaxReturn(3, '图片上传失败', null);
        } else {
            return $this->ajaxReturn(0, '上传成功', config('appconfig.ImgPath') . $publicFolder . '/' . $photo);
        }
    }

    /*
     * 删除图片
     */

    public function delImg() {
        $img = input('param.img');

        $file = new File();
        $mode = config('appconfig.app_production');
        if ($mode) {
            $publicFolder = 'kkmimg';
        } else {
            $publicFolder = 'testkkmimg';
        }
        //http://47.104.83.107/images/testkkmimg/kkmai/ctmstaffqrcode/789658.png
        //sv300/ctmstaffqrcode/9977840295.png;
        // http://47.104.83.107/images/testkkmimg/kkmai/admin/20190911/156820264775736012.png
        $delimg = strstr($img, "kkmai");
        $curlPhoto = $file->curlDeleteImage($publicFolder, $delimg);
        if (!$curlPhoto) {
            return $this->ajaxReturn(3, '删除失败', null);
        } else {
            return $this->ajaxReturn(0, '删除成功', null);
        }
    }

    /*
     * 城市商圈
     */

    public function tradingList() {
        $cityList = TradingCity::field('id,city_id,city_name as name')->select();
        foreach ($cityList as $k => $v) {
            $trading = TradingLabel::where('city_id', '=', $v['city_id'])->select();
            $cityList[$k]['trading'] = $trading;
        }
        return $this->ajaxReturn(0, '查询成功', $cityList);
    }

    /*
     * 城市三级联动
     */

    public function CityList() {
        $prov = ProvCityArea::where('pid', '=', 0)
                ->where('level', '=', 1)
                ->field('id,shortname,name,merger_name') 
                ->select();
        foreach ($prov as $key => $val) {
            $city = ProvCityArea::where('pid', '=', $val['id'])
                    ->where('level', '=', 2)
                    ->field('id,shortname,name,merger_name')
                    ->select();
            foreach ($city as $k => $v) {
                $area = ProvCityArea::where('pid', '=', $v['id'])
                        ->where('level', '=', 3)
                        ->field('id,shortname,name,merger_name')
                        ->select();
                $city[$k]['city'] = $area;
            }
            $prov[$key]['city'] = $city;
        }
        return $this->ajaxReturn(0, '查询成功', $prov);
    }

    /*
     * 店铺图片分类
     * author xfwed 
     * date 2019.09.28
     */

    public function imgClassList($id = '') {//1门头照、2大厅、3美容间 4厕所 5休息区	
        $list = [
            [
                'id' => 1,
                'name' => '门头照'
            ],
            [
                'id' => 2,
                'name' => '大厅'
            ],
            [
                'id' => 3,
                'name' => '美容间'
            ],
            [
                'id' => 4,
                'name' => '厕所'
            ],
            [
                'id' => 5,
                'name' => '休息区'
            ],
        ];
        if ($id) {
            foreach ($list as $k => $v) {
                if ($id == $v['id']) {
                    return $v['name'];
                }
            }
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 店铺投入分类
     * author xfwed 
     * date 2019.09.28
     */

    public function StoreCostInputList($id = '') {//1房租 2月水电费 3设备 4装修 5产品		
        $list = [
            [
                'id' => 1,
                'name' => '房租'
            ],
            [
                'id' => 2,
                'name' => '月水电费'
            ],
            [
                'id' => 3,
                'name' => '设备'
            ],
            [
                'id' => 4,
                'name' => '装修'
            ],
            [
                'id' => 5,
                'name' => '产品'
            ],
            [
                'id' => 6,
                'name' => '耗材'
            ],
        ];
        if ($id) {
            foreach ($list as $k => $v) {
                if ($id == $v['id']) {
                    return $v['name'];
                }
            }
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /* 测试推送 */

    public function test() {
        $ApiPath = config('appconfig.ApiPath8890');
        $stafflist = Staff::where('city', 2)->column('telephone');

        $postFields['content'] = '很兴奋地告诉您又有一个新店铺入驻了，赶快去绑定吧';
        $postFields['type'] = 0; //0新店上新7提现
        $postFields['titleId'] = 1; //1店铺动态2预约消息3营收消息
        $postFields['otherId'] = 0; //新店上新就是0
        $postFields['staffId'] = '';
        $postFields['storeId'] = 14;
        if (count($stafflist) == 1) {
            $postFields['phone'] = $stafflist[0];
            $relust = Utils::curlPost($ApiPath . 'push/pushMessage/', json_encode($postFields));
            print_r($relust);
        } elseif (count($stafflist) > 1) {
            $postFields['phones'] = $stafflist;
            $relust = Utils::curlPost($ApiPath . 'push/pushMessageList/', json_encode($postFields));
            print_r($relust);
        }
    }

    /* 获取提现账号信息 */

    public function tixianInfo() {
        $pay_info['WIDemail'] = config('adminconfig.WIDemail');
        $pay_info['WIDaccount_name'] = config('adminconfig.WIDaccount_name');
        $pay_info['WIDpay_date'] = date('Ymd', time());

        return $this->ajaxReturn(0, '查询成功', $pay_info);
    }

    /*
     * 本店美容师（筛选用）
     * author xfwed
     * date 2019.11.27
     */

    public function storeStaff() {
        $rule = [
            'store_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return $this->ajaxReturn(1, $error, null);
        }
        $store_id = $all['store_id'];
        $stafflist = StaffBindStore::alias('b')
                ->join('staff s', 's.id = b.staff_id', 'left')
                ->where('b.store_id', $store_id)
                ->field('b.staff_id,s.staff_name')
                ->select();
        $list[0]['staff_id'] = 0;
        $list[0]['staff_name'] = '全部推广';
        $list[1]['staff_id'] = -1;
        $list[1]['staff_name'] = '店铺推广';
        foreach ($stafflist as $k => $v) {
            $list[$k + 2]['staff_id'] = $v['staff_id'];
            $list[$k + 2]['staff_name'] = $v['staff_name'];
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 客户分类（筛选用）
     * author xfwed
     * date 2019.11.26
     */

    public function storeCtmType() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        //0全部1访客2未预约3新客户4老客户5开通特权6体验特权7收藏本店
        $staff_id = $all['staff_id'];//0全部-1店铺分享 
        $store_id = $all['store_id'];

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
              $a7 = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.id) > 0";
              $o = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= o.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
              $orderwhere['o.staff_id'] = 0;
          }  else {
              $where['s.staff_id'] = $staff_id;
              $orderwhere['o.staff_id'] = $staff_id;
          }    
        } 
        $orderwhere = isset($orderwhere)?$orderwhere:[];
        $a = isset($a) ? $a : '';
        $a7 = isset($a7) ? $a7 : '';
        $o = isset($o) ? $o : '';
        $where['s.store_id'] = $store_id;
        $allList = StoreCtm::alias('s')
                ->join('staff st', 'st.id = s.staff_id', 'left')
                ->where($where)
                ->where($a)
                //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[0]['id'] = 0;
        $data[0]['name'] = '全部('.$allList.')';
        //tab数量
        $noExpense = StoreCtm::alias('s')
                ->where($where)
                ->where($a)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[1]['id'] = 1;
        $data[1]['name'] = '访客(' . $noExpense . ')';
        $noyuyyue = StoreCtm::alias('s')
                ->where($where)
                ->where($a)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) > 0")
                ->where("(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `store_id`=".$store_id." and `status`>1 and `ctm_id`=s.ctm_id)=0")
                ->group('s.ctm_id')
                ->count();
        $data[2]['id'] = 2;
        $data[2]['name'] = '未预约(' . $noyuyyue . ')';
        $new = Order::alias('o')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where($o)
                ->where($orderwhere)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                ->group('o.ctm_id')
                ->count();
        $data[3]['id'] = 3;
        $data[3]['name'] = '初次消费(' . $new . ')';
        $old = Order::alias('o')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where($orderwhere)
                ->where($o)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                ->group('o.ctm_id')
                ->count();
        $data[4]['id'] = 4;
        $data[4]['name'] = '老客户(' . $old . ')';
        $special1 = Order::alias('o')//本店开通
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where('o.type', 4)
                ->where('o.mini_shop_id',0)
                ->where($o)
                ->where($orderwhere)
                ->group('ctm_id')
                ->count();
        $special2 = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('cs.type',3)
                ->where($where)
                ->where($a)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->group('s.ctm_id')
                ->count();
        $special3 = Order::alias('o')//小店转介绍
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where('o.type', 4)
                ->where('o.mini_shop_id','<>',0)
                ->where($orderwhere)
                ->where($o)
                ->group('ctm_id')
                ->count();
        $special4 = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('c.source',1)
                ->where($where)
                ->group('s.ctm_id')
                ->count();
        $special = $special1+$special2+$special3+$special4;
        $data[5]['id'] = 5;
        $data[5]['name'] = '特权用户(' . $special . ')';
        $expSpecial = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id')
                ->where('c.vip_status', 3)
                ->where($where)
                ->where($a)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->group('s.ctm_id')
                ->count();
        $data[6]['id'] = 6;
        $data[6]['name'] = '体验特权(' . $expSpecial . ')';
        $smallshop = Custom::alias('s')
                ->where('s.smallshop_storeid',$store_id)
                ->where('s.status', 2)
                ->where($a7)
                ->count();
        $data[7]['id'] = 7;
        $data[7]['name'] = '开通i美优优(' . $smallshop . ')';
        $ctm_collect = CtmCollect::alias('co')
                ->join('custom s', 's.id = co.ctm_id', 'left')
                ->where('co.pro_id', $store_id)
                ->where('co.type', 3)
                ->where($a7)
                ->count();
        $data[8]['id'] = 8;
        $data[8]['name'] = '收藏本店(' . $ctm_collect . ')';
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 特权客户分类（筛选用）
     * author xfwed
     * date 2020.03.25
     */

    public function storeSpecialCtmType2020() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
            'type.require' => 'require',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id'];//0全部-1店铺分享 
        $store_id = $all['store_id'];
        $type = $all['type'];

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
              $a7 = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.id) > 0";
              $o = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= o.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
              $orderwhere['o.staff_id'] = 0;
          }  else {
              $where['s.staff_id'] = $staff_id;
              $orderwhere['o.staff_id'] = $staff_id;
          }    
        } 
        $orderwhere = isset($orderwhere)?$orderwhere:[];
        $a = isset($a) ? $a : '';
        $a7 = isset($a7) ? $a7 : '';
        $o = isset($o) ? $o : '';
        $where['s.store_id'] = $store_id;
        if($type == 5){
            $special1 = Order::alias('o')//本店开通
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where('o.type', 4)
                    ->where('o.mini_shop_id',0)
                    ->where($o)
                    ->where($orderwhere)
                    ->group('ctm_id')
                    ->count();
            $special2 = StoreCtm::alias('s')
                    ->join('custom c', 'c.id = s.ctm_id','left')
                    ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                    ->where('c.vip_status', 2)
                    ->where('cs.type',3)
                    ->where($where)
                    ->where($a)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->count();
            $special3 = Order::alias('o')//小店转介绍
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where('o.type', 4)
                    ->where('o.mini_shop_id','<>',0)
                    ->where($orderwhere)
                    ->where($o)
                    ->group('ctm_id')
                    ->count();
            $special4 = StoreCtm::alias('s')//转入客户特权
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('c.source',1)
                ->where($where)
                ->group('s.ctm_id')
                ->count();
            $data[0]['id'] = 51;
            $data[0]['name'] = '本店开通(' . $special1 . ')';
            $data[1]['id'] = 52;
            $data[1]['name'] = '逆行者活动(' . $special2. ')';
            $data[2]['id'] = 53;
            $data[2]['name'] = '小店转介绍(' . $special3 . ')';
            $data[3]['id'] = 54;
            $data[3]['name'] = '转入客户特权(' . $special4 . ')';
        }  else {
            $data = [];
        }
        
        return Utils::ajaxReturn(0, '成功', $data);
    }

    /*
     * 商品品类分类
     * author xfwed
     * date 2019-12-12
     */

    public function goodsOtherCategory() {
        $type = input('param.type'); //类型 1品类2适用肤质	
        $list = GoodsOtherCategory::where('type', $type)->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 商品列表
     * author xfwed
     * date 2019-12-12
     */

    public function goodsList() {
        $item_id = input('param.item_id');
        $goodsids = ItemToGoods::where('item_id', $item_id)->column('goods_info_id');
        $list = Db::table('goods_info')
                ->where('valid', 1)
                ->where('verifyStatus', 2)
                ->where('is_smallshopsell',1)
                ->field('id,name')
                ->select();
        foreach ($list as $k => $v) {
            if(in_array($v['id'], $goodsids)){
               $list[$k]['opt'] = 1; 
            }  else {
                $list[$k]['opt'] = 2; 
            }
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 逆行者审核失败原因
     * author xfwed 
     * date 2020.03.05
     */

    public function medicalWhy() {	
        $list = [
            [
                'id' => 1,
                'failwhy' => '地址不全，需填写到具体门牌号'
            ],
            [
                'id' => 2,
                'failwhy' => '证件不符，需重新上传清晰图片'
            ],
            [
                'id' => 3,
                'failwhy' => '资质不符'
            ],
            [
                'id' => 4,
                'failwhy' => '无人接听，无法核实本人及收货信息'
            ],
            [
                'id' => 5,
                'failwhy' => '请上传证件照'
            ],
            
        ];
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    public function getxcx(){
        //生成店铺小程序码
        $file = new File();
        $xcxcode = $file->generateXcxcode(49);
 
        $qrcodeResult = \app\admin\model\Store::where('id', '=', 49)->update([
            'xcxcode' => $xcxcode,
        ]);
    }
    
    /*
     * 顶部通知退款提醒
     * author xfwed 
     * date 2020.06.17
     */
    public function refundRemind(){
        //$item = Yuyue::where('refund_status',1)->count();
        $subsql = Db::table('ai_program_refund')
                //->where('id','in',"SELECT MAX('id') FROM ai_program_refund GROUP BY 'yuyue_id'")
                ->where("id in (SELECT MAX(`id`) FROM `ai_program_refund` GROUP BY `yuyue_id`)")
                ->field('yuyue_id,status,add_time')
                ->buildSql();
        $item = Db::table('ai_yuyue')->alias('y')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->join('item i', 'i.id = y.item_id', 'left')
                ->join('order o', 'o.id = y.order_id', 'left')
                ->join('store s', 's.id = y.store_id', 'left')
                ->join('staff st', 'st.id = y.staff_id', 'left')
                ->join([$subsql => 'p'],'p.yuyue_id=y.id','left')
                ->where('p.status',5)
                ->group('y.id')
                ->count();
        
        $where['s.status'] = 1;
        $where['o.is_refund'] = 1;
        $goods = Db::table('pay_order_goods')->alias('o')
                ->join('sell_goods_refund s', 's.pay_orders_goods_id = o.id', 'left')
                ->where($where)
                ->count();
        $data['itemRefundNum'] = $item;
        $data['goodsRefundNum'] = $goods;
        return $this->ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 店铺列表(代理绑定店铺时候用)
     * author xfwed
     * date 2019-12-12
     */

    public function storeList() {
        $id = input('param.id');
        //没有绑定代理的店铺
        $list = Store::where('agency_id','in','0,'.$id)
                ->where('is_del',1)
                ->where('auditstore',1)
                ->field('id,store_name,agency_id')
                ->select();
        foreach ($list as $k => $v){
            if($v['agency_id'] != 0){
                $list[$k]['opt'] = 1; 
            }  else {
                $list[$k]['opt'] = 2; 
            }
        }
        
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    /*
     * 店铺列表(代理 列表筛选用)
     * author xfwed
     * date 2020-09-14
     */

    public function agencyStoreList() {
        $agency_id = input('param.agency_id');
        $store_ids = Store::where('agency_id',$agency_id)->column('id');
        $store_ids = implode(',', $store_ids);
        
        $list = Store::where('id','in',$store_ids)
                ->where('is_del',1)
                ->where('auditstore',1)
                ->field('id,store_name,agency_id')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 美容师列表(代理 列表筛选用)
     * author xfwed
     * date 2020-09-14
     */

    public function agencyStaffList() {
        $agency_id = input('param.agency_id');
        $store_ids = Store::where('agency_id',$agency_id)->column('id');
        $store_ids = implode(',', $store_ids);
        $list = StaffBindStore::alias('sb')
                ->join('staff s','s.id = sb.staff_id','left')
                ->where('sb.store_id','in',$store_ids)
                ->field('s.id,s.staff_name')
                ->select();
        if($agency_id == 26){
            $num = count($list);
            $list[$num]['id'] = 533;
            $list[$num]['staff_name'] = '任俊青';
            $list[$num+1]['id'] = 535;
            $list[$num+1]['staff_name'] = '王静';
            $list[$num+2]['id'] = 336;
            $list[$num+2]['staff_name'] = '杨阳';
            $list[$num+3]['id'] = 419;
            $list[$num+3]['staff_name'] = '牟兰波';
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 大牌分类
     * author xfwed
     * date 2020-11-24
     */

    public function bigBrandList() {
        $list = Db::table('sys_dictionary')
                ->where('type','BIG_BRAND')
                ->where('status',0)
                ->where('active',0)
                ->field('code,view')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 大牌商品列表
     * author xfwed
     * date 2020-11-25
     */

    public function bigBrandGoodsList() {
        $bigstar_code = input('param.bigstar_code');
        if($bigstar_code){
            $where['gi.bigstar_code'] = $bigstar_code;
        }
        $where['gi.valid'] = 1;
        $where['gi.is_bigstar'] = 1;
        $list = Db::table('goods_info')
                ->alias('gi')
                ->join(['goods_info_detail'=>'gid'],'gid.goodsId = gi.id','left')
                ->where($where)
                ->order('gid.price desc')
                ->group('gid.goodsId')
                ->field('gi.id,gi.name,gi.bigstar_code,gid.oriPrice,gid.price')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
     /*
     * 最优组合选择项目列表
     * author xfwed
     * date 2020-12-09
     */
    public function bestGroupItemList(){
        $item_id = input('param.item_id');
        $list = Item::where('id','<>',$item_id)
                ->where('status',2)
                ->where('is_del',1)
                ->where('is_yimei',2)
                ->field('id,item_name')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 添加美容师团队选择队长
     * author xfwed
     * date 2021-1-12
     */
    public function captainList(){
        $type = input('param.type');//1选队长  2选区长
        if($type == 1){
            //$where['staff_rank'] = ['in','4,5'];
        }  else {
            $where['staff_rank'] = ['in','5,6'];
        }
        $where['staff_on'] = 1;
        $where['status'] = 1;
        $where['staff_is_creator'] = 0;
        $list = Staff::where($where)->field('id,staff_name')->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 添加团队成员选择美容师
     * author xfwed
     * date 2021-1-18
     */
    public function teamStaffList(){
        $where['staff_on'] = 1;
        $where['status'] = 1;
        $where['staff_is_creator'] = 0;
        $staff_ids = Db::name('staff_team_details')->where('status',1)->column('staff_id');
        $where['id'] = ['not in',$staff_ids];
        $list = Staff::where($where)->field('id,staff_name')->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 添加美容师团队选择店铺
     * author xfwed
     * date 2021-1-18
     */
    public function tradingStoreList(){
        $where['auditstore'] = 1;
        $where['is_del'] = 1;
        //$where['is_open'] = 1;
        $store_ids = Db::name('staff_team_store')->where('status',1)->column('store_id');
        //$where['id'] = ['not in',$store_ids];
        $list = Store::where($where)->field('id,store_name,type')->select();
        foreach ($list as $k => $v){
            if(in_array($v['id'], $store_ids)){
                $list[$k]['status'] = TRUE;//1是2否选过了
            }  else {
                $list[$k]['status'] = FALSE;//1是2否选过了
            }
            if($v['type'] == 1){
                $typename = '品牌店';
            }elseif ($v['type'] == 2) {
                $typename = '授权店';
            }
            $list[$k]['store_name'] = $v['store_name'].'('.$typename.')';
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 修改美容师团队头像
     * author xfwed
     * date 2021-1-22
     */
    public function saveTeamImg(){
        $id = input('param.id');//团队id
        $team_img = input('param.team_img');
        Db::name('staff_team')->where('id',$id)->update(['team_img'=>$team_img]);
        return $this->ajaxReturn(0, '修改成功', null);
    }
    
    /*
     * 可约店铺列表(体验卡添加可约时间时候用)
     * author xfwed
     * date 2021-06-02
     */

    public function cardStoreList() {
        $ids = Db::table('sys_dictionary')
                ->where('type','EXPERIENCE_STORE_IDS')
                ->value('view');
        $list = Store::where('id','in',$ids)
                ->field('id,store_name')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 美容师列表
     * author xfwed
     * date 2021-07-13
     */

    public function staffList() {
        $list = Db::name('staff')
                ->where('staff_is_creator',0)
                ->where('status',1)
                ->field('id,staff_name,nickname,telephone')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    /*
     * 代金券列表（添加接龙活动用）
     * author xfwed
     * date 2021-08-09
     */

    public function couponList() {
        $type = input('param.type');
        if($type == 2){
            $where['type'] = ['in','2,5'];
        }  else {
            $where['type'] = $type;
        }
        $list = Db::name('coupon')
                ->where($where)
                ->where('mature','>',date('Y-m-d H:i:s'))
                ->where('issue',1)
                ->field('id,name,money,reach')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 红包列表（添加接龙活动用）
     * author xfwed
     * date 2021-08-09
     */

    public function redPacketSetupList() {
        $list = Db::name('red_packet_setup')
                ->where('type',4)
                ->where('left_num','>',0)
                ->where('status',1)
                ->field('id,name')
                ->select();
        return $this->ajaxReturn(0, '查询成功', $list);
    }
}
